package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PayMoneyLogAdapter;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseSelectObject;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.PayMoneyLog;
import com.isunland.managesystem.entity.PayMoneyLogListOriginal;
import com.isunland.managesystem.entity.PayMoneyLogQueryParams;
import com.isunland.managesystem.entity.SimpleListParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/21 14:59
 */
public class PayMoneyLogListFragment extends SimpleListFragment<PayMoneyLog> {

  /** 查询 */
  private static final int REQUEST_QUERY = 1;
  /** 开始日期 */
  private String mStartDate =
      MyDateUtil.format(MyDateUtil.getBeforeOneMonth(), MyDateUtil.TYPE_DATE);
  /** 结束日期 */
  private String mEndDate = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
  /** 类型 */
  private String mType;
  /** 订单编号 */
  private String mOrderNo = "";
  private HashMap<String, BaseSelectObject> mMap;
  private PayMoneyLogQueryParams mParams;

  @Override protected SimpleListParams initParams() {
    mMap = new HashMap<>();
    mMap.put(PayMoneyLog.TYPE_LEASE,
        new BaseSelectObject("", getString(R.string.leaseEquip), PayMoneyLog.TYPE_LEASE));
    mMap.put(PayMoneyLog.TYPE_WORK,
        new BaseSelectObject("", getString(R.string.taskOrder), PayMoneyLog.TYPE_WORK));
    mMap.put(PayMoneyLog.TYPE_ORDER,
        new BaseSelectObject("", getString(R.string.onlineAppoint), PayMoneyLog.TYPE_ORDER));

    SimpleListParams params = new SimpleListParams();
    params.setClassDetailActivity(PayMoneyLogDetailActivity.class);
    params.setClassOriginal(PayMoneyLogListOriginal.class);
    params.setUrl(ApiConst.URL_QUERY_LIST_PAY_MONEY_LOG);
    params.setTitle(getTitleByType(mType));
    return params;
  }

  @Override public void initData() {
    super.initData();
    mParams = mBaseParams instanceof PayMoneyLogQueryParams ? (PayMoneyLogQueryParams) mBaseParams
        : new PayMoneyLogQueryParams();
    //从list改成pager页面,type需要从外面传过来,其他的不变.
    mType = (!MyStringUtil.isEmpty(mParams.getQueryType())) ? mParams.getQueryType()
        : PayMoneyLog.TYPE_WORK;
    mParams.setStartDate(mStartDate);
    mParams.setEndDate(mEndDate);
    mParams.setQueryType(mType);
    mParams.setOrderNo(mOrderNo);
    mParams.setQueryMap(mMap);
  }

  @Override
  protected BaseAdapter initAdapter(BaseVolleyActivity context, ArrayList<PayMoneyLog> list) {
    return new PayMoneyLogAdapter(context, list);
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("orderType", mType);
    params.put("beginpayTime", mStartDate);
    params.put("endpayTime", mEndDate);
    params.put("bookOrderNo", mOrderNo);
    return params.getParams();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_query:

        PayMoneyLogQueryActivity.newInstance(this, PayMoneyLogQueryActivity.class, mParams,
            REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    switch (requestCode) {
      case REQUEST_QUERY:
        PayMoneyLogQueryParams item =
            (PayMoneyLogQueryParams) data.getSerializableExtra(BaseFragment.EXTRA_PARAMS);
        mType = item.getQueryType();
        mStartDate = item.getStartDate();
        mEndDate = item.getEndDate();
        mOrderNo = item.getOrderNo();
        mParams.setStartDate(mStartDate);
        mParams.setEndDate(mEndDate);
        mParams.setQueryType(mType);
        mParams.setOrderNo(mOrderNo);

        //setTitleCustom(getTitleByType(mType));
        volleyPost();
        break;
      default:
        super.onActivityResult(requestCode, resultCode, data);
    }
  }

  private String getTitleByType(String mType) {
    if (TextUtils.isEmpty(mType)) return null;
    return MyStringUtil.connect(mMap.get(mType).getName(), getString(R.string.payCheck));
  }
}

